package com.dxb.test.thread;

import java.util.concurrent.CountDownLatch;

/**
 * ClassName：com.dxb.test.thread.CountDownLatchABC <br>
 * Description：CountDownLatch <br>
 *
 * @author 戴晓波 <br>
 * Date 2022/2/14/014 16:10 <br>
 * @version v1.0 <br>
 */
public class CountDownLatchABC {
    void print(CountDownLatch current, CountDownLatch next) {
        if (current != null) {
            try {
                //一进来就卡住，等待前一个线程的countDown
                current.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName());
        if (next != null) {
            //让下一个线程从await中醒来
            next.countDown();
        }
    }

    public static void main(String[] args) {
        CountDownLatchABC latchABC = new CountDownLatchABC();
        CountDownLatch latchB = new CountDownLatch(1);
        CountDownLatch latchC = new CountDownLatch(1);

        new Thread(() -> latchABC.print(null, latchB), "A").start();
        new Thread(() -> latchABC.print(latchB, latchC), "B").start();
        new Thread(() -> latchABC.print(latchC, null), "C").start();
    }
}
